package com.vxb.testmvp.test_fragment.activity;

import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.vxb.testmvp.BaseActivity;
import com.vxb.testmvp.R;
import com.vxb.testmvp.test_fragment.FragmentFactory;
import com.vxb.testmvp.test_fragment.LoadingPager;
import com.vxb.testmvp.test_fragment.fm_a.FragmentA;
import com.vxb.testmvp.test_fragment.fm_b.FragmentB;
import com.vxb.testmvp.test_fragment.fm_c.FragmentC;
import com.vxb.testmvp.test_fragment.fm_d.FragmentD;

import static com.vxb.testmvp.R.id.tv_a;
import static com.vxb.testmvp.R.id.tv_b;

public class TestActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView[] tv_bottom = new TextView[4];
    private FragmentA fm_a;
    private FragmentB fm_b;
    private FragmentC fm_c;
    private FragmentD fm_d;

    private static final String STATE_PAGE_ID = "page_id";
    private int mCurrentTabId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        if (savedInstanceState == null) {
            setCurrentFragment(0);
        } else {
            setCurrentFragment(savedInstanceState.getInt(STATE_PAGE_ID, 0));
        }
        initView();

    }

    protected void initView() {

        tv_bottom[0] = (TextView) findViewById(tv_a);
        tv_bottom[1] = (TextView) findViewById(tv_b);
        tv_bottom[2] = (TextView) findViewById(R.id.tv_c);
        tv_bottom[3] = (TextView) findViewById(R.id.tv_d);

        tv_bottom[0].setSelected(true);

        setOnClick(tv_bottom);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_PAGE_ID, mCurrentTabId);
    }


    private void setOnClick(TextView[] tvs) {
        for (TextView tv : tvs) {
            tv.setOnClickListener(this);
        }
    }

   /* private void setOnClick(View... views) {
        for (View view : views) {
            view.setOnClickListener(this);
        }
    }*/

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_a:
                setBottomColor(0);
                break;
            case R.id.tv_b:
                setBottomColor(1);
                break;
            case R.id.tv_c:
                setBottomColor(2);
                break;
            case R.id.tv_d:
                setBottomColor(3);
                break;
        }
    }

    private void setBottomColor(int i) {
        for (int j = 0; j < 4; j++) {
            tv_bottom[j].setSelected(false);
        }
        tv_bottom[i].setSelected(true);

        setCurrentFragment(i);

    }

    private void setCurrentFragment(int i) {
        FragmentTransaction be = getFragmentManager().beginTransaction();

        switch (i) {
            case 0:

                if (fm_a == null) {
                    fm_a = (FragmentA) FragmentFactory.createFragment(i);
                    be.add(R.id.fm_content, fm_a, "A");
                } else {
                    be.show(fm_a);
                }
                if (fm_b != null && !fm_b.isHidden()) {
                    be.hide(fm_b);
                } else if (fm_c != null && !fm_c.isHidden()) {
                    be.hide(fm_c);
                } else if (fm_d != null && !fm_d.isHidden()) {
                    be.hide(fm_d);
                }

                break;
            case 1:

                if (fm_b == null) {
                    fm_b = (FragmentB) FragmentFactory.createFragment(i);
                    be.add(R.id.fm_content, fm_b, "B");
                } else {
                    be.show(fm_b);
                }

                if (fm_a != null && !fm_a.isHidden()) {
                    be.hide(fm_a);
                } else if (fm_c != null && !fm_c.isHidden()) {
                    be.hide(fm_c);
                } else if (fm_d != null && !fm_d.isHidden()) {
                    be.hide(fm_d);
                }

                break;
            case 2:

                if (fm_c == null) {
                    fm_c = (FragmentC) FragmentFactory.createFragment(i);
                    be.add(R.id.fm_content, fm_c, "C");
                } else {
                    be.show(fm_c);
                }

                if (fm_a != null && !fm_a.isHidden()) {
                    be.hide(fm_a);
                } else if (fm_b != null && !fm_b.isHidden()) {
                    be.hide(fm_b);
                } else if (fm_d != null && !fm_d.isHidden()) {
                    be.hide(fm_d);
                }

                break;
            case 3:
                if (fm_d == null) {
                    fm_d = (FragmentD) FragmentFactory.createFragment(i);
                    be.add(R.id.fm_content, fm_d, "D");

                } else {
                    be.show(fm_d);
                }

                if (fm_a != null && !fm_a.isHidden()) {
                    be.hide(fm_a);
                } else if (fm_b != null && !fm_b.isHidden()) {
                    be.hide(fm_b);
                } else if (fm_c != null && !fm_c.isHidden()) {
                    be.hide(fm_c);
                }

                break;
        }
        be.commit();
        mCurrentTabId = i;
    }

}
